/*
 * 	name: AddressBean
 *  author: schure + all authors of Eventbean
 *
 *  
 *  Falls jemand etwas in die Datenbank schreiben will, muss er die Setmethoden auskommentieren und umändern.
 * 
 * */


package at.fhj.itm10.ticketmaster.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.faces.bean.RequestScoped;

import at.fhj.itm10.ticketmaster.dao.AddressDAO;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.impl.DAOFactoryImpl;
import at.fhj.itm10.ticketmaster.entities.Address;
import at.fhj.itm10.ticketmaster.services.AddressService;
import at.fhj.itm10.ticketmaster.services.ServiceFactory;

@ManagedBean
@RequestScoped
public class AddressBean {

	private AddressDAO dao;
	private Address address;
	private AddressService addressService;

	
	
	
	private List<Address> addressList = new ArrayList<Address>();
	
	
	
	
	private HashMap<String, Address> addresses = new HashMap<String, Address>();

	
	
	public AddressBean() {
		super();
		init();
	}

		
	/*
	 * writes data into HashMap
	 */
	public void initAddresses() {
		
		Address a;
		
		for(int i=0; i < addressList.size(); i++) {
			a= addressList.get(i);
			
			addresses.put((Long.toString(a.getId())), a);
		}
		
	}
	
	/*
	 * grab events for warenkorb.xhtml
	 */
	public Address showAddressInfos(String id) {
		Address addressData = null;
		
		if(addresses.containsKey(id)) {
			addressData = addresses.get(id); 
			
		} else {
			System.err.println("ERROR: No Event with this id -> " + id + "!");
		}
		
		return addressData;
	}
	
	/*
	 * get price for a single Event
	 */
	
	
	/*public String saveNewAddress() {

		// Todo: Eventuell Felder validieren

		try {
			dao.insert(address);
		} catch (DAOException e1) {
			// TODO Auto-generated catch block
			return "Speichern fehlgeschlagen " + e1.getMessage();
		}

		return "Speichern erfolgreich";

	}

	public String editAddress() {

		//ToDO: Check ob mindestens 72h vor beginn
		
		try {
			dao.update(address);
		} catch (DAOException e1) {
			// TODO Auto-generated catch block
			return "Speichern fehlgeschlagen " + e1.getMessage();
		}

		return "Speichern erfolgreich";

	
		
	}*/

	@PostConstruct
	public void init() {
		try {
			// getDAO
			DAOFactoryImpl daoFactoryImpl;
			daoFactoryImpl = new DAOFactoryImpl();
			
			ServiceFactory factory = new ServiceFactory();
			addressService = factory.getAddressService();

			addressService.setAddressDao(daoFactoryImpl.getAddressDAO());
			setDao(addressService.getAddressDao());
			
			initAddresses();
			addressList = dao.findAll();
			// insert test data
			// dao.insert(new
			// Event(1,"TestEvent1","2012-11-12 12:00:00",1,1,1,100.00,999,1,0));
			// dao.insert(new
			// Event(2,"TestEvent2","2012-11-13 12:00:00",1,1,1,50.00,888,1,0));

			// get test data
			// setEvent(dao.findById(1));

			// print test data to console
			//System.err.println(getEvent().toString());
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	private void setDao(AddressDAO dao) 
	{
		this.dao = dao;
	}

	public AddressDAO getDao() 
	{
		return dao;
	}

	public List<Address> getAdresslist() {
		return addressList;
	}


	public void setAdresslist(List<Address> addressList) {
		this.addressList = addressList;
	}


	public Address getAddress() {
		return address;
	}


	public void setAddress(Address address) {
		this.address = address;
	}
	
}
	  

	